package com.supreme.cacheserver.controller;

import com.supreme.cacheserver.service.BloomFilterService;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(value = "bloom")
public class BloomFilterController {

    @Resource
    private BloomFilterService bloomFilterService;

    @GetMapping(value = "putItem/{key}")
    public AjaxResp putItem(@PathVariable(value = "key") String key) {
        boolean result = bloomFilterService.putItemToBloomFilter(key);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "checkItem/{key}")
    public AjaxResp checkItem(@PathVariable(value = "key") String key) {
        boolean result = bloomFilterService.checkItemInBloomFilter(key);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "debugRedisBloomFilter")
    public AjaxResp debugRedisBloomFilter() {
        CompletableFuture.runAsync(() -> bloomFilterService.debugRedisBloomFilter());
        return AjaxResp.instance();
    }

    @GetMapping(value = "createRedisBloomFilter/{key}/{capacity}/{errorRate}")
    public AjaxResp createRedisBloomFilter(@PathVariable(value = "key") String key,
                                           @PathVariable(value = "capacity") Long capacity,
                                           @PathVariable(value = "errorRate") Double errorRate) {
        bloomFilterService.createRedisBloomFilter(key, capacity, errorRate);
        return AjaxResp.instance();
    }

    @GetMapping(value = "putRedisBloomFilter/{key}/{value}")
    public AjaxResp putRedisBloomFilter(@PathVariable(value = "key") String key,
                                        @PathVariable(value = "value") String value) {
        boolean result = bloomFilterService.putRedisBloomFilter(key, value);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "checkRedisBloomFilter/{key}/{value}")
    public AjaxResp checkRedisBloomFilter(@PathVariable(value = "key") String key,
                                          @PathVariable(value = "value") String value) {
        boolean result = bloomFilterService.checkRedisBloomFilter(key, value);
        return AjaxResp.instance(result);
    }
}
